import matplotlib.pyplot as plt
import matplotlib.patches as patches
import ast

# 场地参数
FIELD_LENGTH = 220  # x方向总长（-110~110）
FIELD_WIDTH = 180   # y方向总宽（-90~90）
GOAL_WIDTH = 40
GOAL_DEPTH = 20
PENALTY_X = -110 + 37.5
CENTER_X = 0
CENTER_Y = 0

# 机器人和球的渲染参数
ROBOT_RADIUS = 3
BALL_RADIUS = 2


def draw_field(ax):
    # 绘制外框
    ax.add_patch(patches.Rectangle((-110, -90), 220, 180, fill=False, linewidth=2))
    # 绘制中线
    ax.plot([0, 0], [-90, 90], 'k--', lw=1)
    # 绘制中圈
    center_circle = patches.Circle((0, 0), 25, fill=False, linestyle='--', linewidth=1)
    ax.add_patch(center_circle)
    # 绘制球门区
    ax.add_patch(patches.Rectangle((-110, -20), 10, 40, fill=False, linewidth=1))
    ax.add_patch(patches.Rectangle((100, -20), 10, 40, fill=False, linewidth=1))
    # 绘制禁区
    ax.add_patch(patches.Rectangle((-110, -40), 30, 80, fill=False, linewidth=1))
    ax.add_patch(patches.Rectangle((80, -40), 30, 80, fill=False, linewidth=1))
    # 绘制点球点
    ax.plot([-72.5, 72.5], [0, 0], 'ro', markersize=3)
    # 绘制角球点
    for x in [-110, 110]:
        for y in [-90, 90]:
            ax.plot(x, y, 'ko', markersize=3)
    # 设置坐标轴
    ax.set_xlim(-120, 120)
    ax.set_ylim(-95, 95)
    ax.set_aspect('equal')
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_title('Soccer Field')
    ax.grid(True, linestyle=':')


def render_positions(robot_positions, ball_position):
    """
    robot_positions: list of (x, y), 长度为5
    ball_position: (x, y)
    """
    fig, ax = plt.subplots(figsize=(10, 5))
    draw_field(ax)
    # 绘制机器人
    for i, (x, y) in enumerate(robot_positions):
        ax.add_patch(patches.Circle((x, y), ROBOT_RADIUS, color='b', alpha=0.7))
        ax.text(x, y+4, f'R{i}', color='b', ha='center', fontsize=8)
    # 绘制球
    bx, by = ball_position
    ax.add_patch(patches.Circle((bx, by), BALL_RADIUS, color='orange', alpha=0.9))
    ax.text(bx, by+4, 'Ball', color='orange', ha='center', fontsize=8)
    plt.show()


def input_positions_list():
    print("请输入一个Python风格的嵌套列表，格式如：\n[[x1, y1, a1], [x2, y2, a2], [x3, y3, a3], [x4, y4, a4], [x5, y5, a5], [xb, yb, ab]]\n前五个为机器人，最后一个为球，角度a可忽略")
    while True:
        try:
            s = input("请输入坐标列表：\n")
            pos_list = ast.literal_eval(s)
            if not (isinstance(pos_list, list) and len(pos_list) == 6):
                print("必须为6个元素的列表！")
                continue
            robots = [(float(x), float(y)) for x, y, _ in pos_list[:5]]
            ball = (float(pos_list[5][0]), float(pos_list[5][1]))
            return robots, ball
        except Exception as e:
            print("输入格式有误，请重新输入。错误：", e)


if __name__ == '__main__':
    robots, ball = input_positions_list()
    render_positions(robots, ball) 